package com.smh.test2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//反射可以跟配置文件结合的方式，动态的创建对象，并调用方法
        //1.读取配置文件中的信息
        Properties prop = new Properties();
        File file = new File("day31-code\\file\\prop.properties");
        FileInputStream fis = new FileInputStream(file);

        prop.load(fis);
        //获取全类名和方法名
        String classname = (String) prop.get("classname");
        String methodName = (String) prop.get("method");

        //利用反射创建对象运行方法
        Class<?> c = Class.forName(classname);
        //创建对象
        Constructor<?> con = c.getDeclaredConstructor();
        con.setAccessible(true);
        Object o = con.newInstance();
        System.out.println(o);
        //获取方法并运行
        Method method = c.getMethod(methodName);
        method.setAccessible(true);
        Object invoke = method.invoke(o);
        System.out.println(invoke);


        System.out.println("------------------------------");
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }


    }
}
